#导入相应的模块
import pygame
from pygame.locals import *
import time,random

class HeroPlane:
	'''玩家飞机(英雄)'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./me.png")
		self.bullet_list=[]

	def display(self):
		'''绘制玩家飞机'''
		#绘制子弹
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)
		self.screen.blit(self.image,(self.x,self.y))

	def move_left(self):
		'''左移动'''
		self.x -= 5
		if self.x <=0:
			self.x =0

	def move_right(self):
		'''右移动'''
		self.x += 5
		if self.x>=406:
			self.x=406

	def fire(self):
		'''主角开火'''
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		print(len(self.bullet_list))

	def takinghits(self,enemylist):
		for enemyaircraft in enemylist:
			for ebs in enemyaircraft.enemybullet_list:
				if ebs.x>self.x+12 and ebs.x<self.x+92 and ebs.y>self.y+20 and ebs.y<self.y+60:
					enemyaircraft.enemybullet_list.remove(ebs)

					self.image=pygame.image.load("./explode7.png")
					self.screen.blit(self.image,(self.x,self.y))

					print("受到攻击")
					print("游戏结束")
					exit()
					return True
				

class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x+53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		self.y -= 10
		if self.y <=-20:
			return True

class Enemybullet:
	'''敌人子弹类'''
	def __init__(self,screen_temp,x,y):
		#self.x = self.x+53
		self.x = x+53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def embmove(self):
		self.y += 5 #60很快
		if self.y >=600:
			return True

class EnemyPlane:
	'''敌机类'''
	def __init__(self,screen_temp):                
		self.x=random.choice(range(408))
		self.x_location=self.x
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./e"+str(random.choice(range(3)))+".png")
		self.enemybullet_list=[]
                
	def display(self):
		'''绘制敌人子弹'''
		for emb in self.enemybullet_list:
			emb.display()	
			if emb.embmove():
				self.enemybullet_list.remove(emb)	
		self.screen.blit(self.image,(self.x,self.y))	

	def move(self,hero):
		self.y += 4
		#敌机出屏幕
		if self.y>568:
			return True
		# 遍历所有子弹，并执行碰撞检测
		for bo in hero.bullet_list:
			if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
				self.image=pygame.image.load("./explode6.png")
				self.screen.blit(self.image,(self.x,self.y))
				hero.bullet_list.remove(bo)
				return True

	def enemyfire(self):
		self.enemybullet_list.append(Enemybullet(self.screen,self.x_location,self.y))
		print(len(self.enemybullet_list))

class Explosion:
	def __init__(self,screen_temp,x,y):
		self.screen = screen_temp
		self.image = pygame.image.load("./explode6.png")
		self.x=x
		self.y=y
	def display(self):
		self.screen.blit(self.image,(self.x,self.y))

def key_control(hero_temp):
	'''键盘控制函数'''
	#执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()
	#获取按键信息
	pressed_keys = pygame.key.get_pressed()
	#print(pressed_keys)
	#做判断，并执行对象的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("Left...")
		hero_temp.move_left()
		
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("Right...")
		hero_temp.move_right()
		
	if pressed_keys[K_SPACE]:
		print("Space...")
		hero_temp.fire()

def main():
	'''主程序函数'''
	#创建游戏窗口
	screen=pygame.display.set_mode((512,568),0,0)
	#创建一个游戏背景
	background = pygame.image.load("./bg2.jpg")
	# 创建玩家飞机（英雄）
	m=-968
	enemylist=[]#存放敌机的列表
	enemybulletlist=[]
	hero = HeroPlane(screen)
	while True:
		#绘制画面
		screen.blit(background,(0,m))
		m+=2
		if m>=-200:
			m=-968
		#绘制玩家飞机
		hero.display()
		#执行键盘控制
		key_control(hero)
		#随机绘制敌机
		if random.choice(range(50))==42:
			enemylist.append(EnemyPlane(screen))
		#遍历敌机并绘制移动（也许和开火有关？）
		for em in enemylist:
			em.display()
			if em.move(hero):
				enemylist.remove(em)
			if random.choice(range(20))==1:
				EnemyPlane.enemyfire(em)
		if hero.takinghits(enemylist):
			print("TakingHit!!!")
			print("herodown")
			
			#break
		#更新显示
		pygame.display.update()
		#定时显示
		time.sleep(0.04)

#判断当前是否是主运行程序，并调用
if __name__ == "__main__":
	main()
